package ihm.tabs;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import ihm.overlays.ElementListener;
import ihm.overlays.FriendGroupOverlay;
import ihm.overlays.FriendOverlay;
import ihm.overlays.ListElementsOverlay;
import ihm.widgets.Element;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

/**
 * Tab displaying the group info.
 * @author Nicolas Yzet <nicolas.yzet@gmail.com>
 *
 */
public class GroupsTab extends Composite {

	private final Map<String, Collection<FriendData>> friendsMap = new HashMap<String, Collection<FriendData>>();
	private final ListElementsOverlay groupsPage;
	
	/**
	 * Constructor. All graphics components are initialized here.
	 * @param father composite.
	 */
	public GroupsTab(Composite father)
	{
		super(father, SWT.NONE);
		final StackLayout stackLayout = new StackLayout();
		setLayout(stackLayout);
		
		groupsPage = new ListElementsOverlay(this);
		final FriendGroupOverlay groupPage= new FriendGroupOverlay(this);
		final FriendOverlay friendPage = new FriendOverlay(this);
		
		// setup event handling
		groupsPage.addElementListener(new ElementListener(){
			@Override
			public void elementClicked(Element e) {
				// here we must lookup in the group list and initialize the data
				// to display in the group overview
				groupPage.clear();
				Collection<FriendData> friends = friendsMap.get(e.getUserName());
				for(FriendData fd : friends) {
					groupPage.addFriend(fd.getName(), fd.getPicture());
				}
				
				stackLayout.topControl = groupPage;
				layout();
				groupPage.layout();
			}
		});
		
		groupPage.addFriendListener(new ElementListener() {
			@Override
			public void elementClicked(Element e) {
				// fill the Friend page with info relative to this friend
				// there is more to fill
				friendPage.setUserName(e.getUserName());
				friendPage.setPicture(e.getPicture());
				
				stackLayout.topControl = friendPage;
				layout();
				friendPage.layout();
			}
		});
		
		
		groupPage.addReturnButtonSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				stackLayout.topControl = groupsPage;
				layout();
				groupsPage.layout();
			}
		});
		
		friendPage.addReturnButtonSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				// TODO: switch back to the friend list in the group.
			}
		});
		
		stackLayout.topControl = groupsPage;
	}
	
	/**
	 * TODO: refactor this function to match the interface of the objects
	 * made by lo23.data.
	 * Add a group in the list.
	 * @param groupName name of the group
	 * @param groupPicture picture of the group
	 */
	public void addGroup(String groupName, Image groupPicture, Collection<FriendData> friends) {
		groupsPage.addElement(groupName, groupPicture);
		friendsMap.put(groupName, friends);
	}
}
